package com.mzhmxh.util;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author Chronos
 * @Date 2019/12/4 4:12 下午
 * @Version 1.0
 * @package com.colin.util
 **/
@Component
public class DateUtil {
    public final static String  FORMAT = "yyyy-MM-dd hh:mm:ss";

    public static String getDateTime(){

        return new SimpleDateFormat(FORMAT).format(new Date());
    }
    /**
     *使用Calendar对象计算时间差，可以按照需求定制自己的计算逻辑
     * @param strDate
     * @throws ParseException
     */
    public static int calculateTimeDifferenceByCalendar(String strDate) throws  ParseException {
        Date date = new SimpleDateFormat(FORMAT).parse(strDate);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date);

        int year = c1.get(Calendar.YEAR);
        int oldYear = c2.get(Calendar.YEAR);

        return (year - oldYear);
    }

    public static String getOldDate(Integer integer){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, - integer);
        Date time = c.getTime();
        return new SimpleDateFormat(FORMAT).format(time);
    }
}
